package com.cgq.pro.dao.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import com.cgq.base.dao.impl.BaseDaoImpl;
import com.cgq.pro.dao.PolicyDao;
import com.cgq.pro.entity.Policy;
/**
 * 数据访问组件
 * 国内政策
 * @author chenkaile
 *
 */
@Repository
@SuppressWarnings("rawtypes")
public class PolicyDaoImpl extends BaseDaoImpl<Policy> implements PolicyDao {

	@SuppressWarnings("unchecked")
	public List<Policy> getAllRecords(final int start, final int pageSize,Map<String,
			String> map,String typeid) {
		final StringBuilder hql=new StringBuilder();
		hql.append("from Policy o where o.type="+typeid);
		if(map!=null && map.size()>0){
			hql.append(" and o.dateCre>='"+map.get("startDate")+"'");
			hql.append(" and o.dateCre<='"+map.get("endDate")+"'");
		}
		List list=null;
		try {
			list=hibernateTemplate.executeFind(new HibernateCallback() {

				public List doInHibernate(Session session)
						throws HibernateException, SQLException {
					Query query = session.createQuery(hql.toString()); 
					query.setFirstResult(start);
					query.setMaxResults(pageSize);
					return query.list();
				}
			});
		} catch (Exception e) {
			System.out.println(e.toString());
		}
		
		return list;
	}

	public int getTotal(Map<String, String> map, String typeid) {
		final StringBuilder hql=new StringBuilder();
		hql.append("from Policy o where o.type="+typeid);
		if(map!=null && map.size()>0){
			hql.append(" and o.dateCre>='"+map.get("startDate")+"'");
			hql.append(" and o.dateCre<='"+map.get("endDate")+"'");
		}
		List list=hibernateTemplate.find(hql.toString());
		if(list!=null && list.size()>0)
			return list.size();
		else 
			return 0;
	}
	
}
